export function schemaToHeader (schema) {
  const header = []
  function parse (name, schema, column, level) {
    if (level >= header.length) {
      header.push([])
    }
    const row = header[level]
    let width = 0
    for (const key in schema.properties) {
      if (!schema.properties.hasOwnProperty(key)) continue
      let child = schema.properties[key]
      if (Array.isArray(schema.required) && schema.required.indexOf(key) !== -1) {
        child = Object.assign(child, { required: true })
      }
      const fullname = name ? [name, key].join('.') : key
      let field = { name: fullname, column: column }
      field = Object.assign(field, child)
      if (child.hasOwnProperty('properties')) {
        field.width = parse(fullname, child, column, level + 1)
        delete field.properties
        width += field.width
        column += field.width
      } else if (child.hasOwnProperty('items')) {
        field.width = parse(fullname, child['items'], column, level + 1)
        width += field.width
        column += field.width
      } else {
        width += 1
        column += 1
      }
      row.push(field)
    }
    return width
  }
  parse(null, schema, 0, 0)
  return header
}
export function shrink (obj, skip, prefix) {
  prefix = prefix || ''
  const object = {}
  for (const k in obj) {
    if (obj.hasOwnProperty(k)) {
      if (typeof obj[k] === 'string' || typeof obj[k] !== 'object') {
        object[prefix + k] = obj[k]
      } else {
        Object.assign(object, shrink(obj[k], skip, prefix + k + '_'))
      }
    }
  }
  return object
}
